﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.ServiceModel;
using CommunicationDataContract;
using System.Windows.Media;

namespace CommunicationDesktopApplication
{
    public class DesktopServiceController : INotifyPropertyChanged
    {
        #region Private Fields
        private ServiceHost serviceHost;
        #endregion

        #region Backed Properties
        public static DesktopServiceController Instance{
            get{
                if (instance == null)
                    instance = new DesktopServiceController();

                return instance;
            }
        }
        private static DesktopServiceController instance;

        public Boolean IsWindowsServiceActive
        {
            get
            {
                return isWindowsServiceActive;
            }
            set
            {
                isWindowsServiceActive = value;
                OnPropertyChanged("IsWindowsServiceActive");
            }
        }
        private Boolean isWindowsServiceActive = false;

        public Guid ApplicationId
        {
            get
            {
                return applicationId;
            }
            set
            {
                applicationId = value;
                OnPropertyChanged("ApplicationId");
            }
        }
        private Guid applicationId = Guid.NewGuid();

        public Byte[] ApplicationColor
        {
            get
            {
                return applicationColor;
            }
            set
            {
                applicationColor = value;
                OnPropertyChanged("ApplicationColor");
            }
        }
        private Byte[] applicationColor = GetRandomColor();
        public Guid ApplicationIdLastPing
        {
            get
            {
                return applicationIdLastPing;
            }
            set
            {
                applicationIdLastPing = value;
                OnPropertyChanged("ApplicationIdLastPing");
            }
        }
        private Guid applicationIdLastPing = Guid.Empty;
        #endregion
        #region Calculated Properties
        public Boolean IsListeningEnabled {
            get
            {
                return serviceHost != null && serviceHost.State == CommunicationState.Opened;
            }
        }
        #endregion
        #region Constructors
        public DesktopServiceController()
        {
            
        }
        #endregion
        #region Public Methods 
        public void ListeningStop()
        {
            if (IsListeningEnabled)
            {
                serviceHost.Close();
                serviceHost = null;
            }
            else if (serviceHost != null)
            {
                serviceHost = null;
            }
        }
        public void ListeningStart()
        {
            ListeningStop();
            serviceHost = new ServiceHost(typeof(DesktopCommunicationEndPoint));
            NetNamedPipeBinding binding = new NetNamedPipeBinding(NetNamedPipeSecurityMode.None);
            serviceHost.AddServiceEndpoint(typeof(IDesktopCommunicationEndPointContract), binding, EndpointSettings.GetDesktopApplicationEndpointAddress(DesktopServiceController.Instance.ApplicationId));
            serviceHost.Open();
        }
        #endregion

        #region Static Methods
        private static Byte[] GetRandomColor()
        {
            byte[] result = new byte[4];
            var random = new Random();
            random.NextBytes(result);
            return result;
        }
        #endregion

        #region INotifyPropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }
        #endregion
    }
}
